/**
 * Schedule基础接口
 * 上映计划表作为影院和影片的中间表
 */
const AV = require("leanengine");
const BaseService = require("./baseService");
class Service extends BaseService {
  constructor() {
    const moduleName = "CinemaFilmMap";
    const attrs = {
      // advanceStopMins: "", //"advanceStopMins",
      payDay: "", //"上映日期",
      showAt: "", //"开始时间",
      endAt: "", //"结束时间",
      filmLanguage: "", //"电影语言",
      hallName: "", //"影厅序号",
      imagery: "", //"成影类型",
      isOnsell: "", //"在售",
      marketPrice: "", //"市场价格",
      salePrice: "" //"售价"
    };
    super(moduleName, attrs);
  }

  async add(ctx) {
    try {
      let { filmId } = ctx.params;
      let dependentObj = new AV.Object.createWithoutData("Film", filmId);
      const result = await this.addAndDependent(ctx.params, dependentObj);
      const res = this.addResInfo({
        type: 1,
        msg: "添加上映计划失败",
        result
      });
      return {
        ...res,
        data: result
      };
    } catch (error) {
      return this.addResInfo({ error, msg: "添加上映计划失败" });
    }
  }

  async findById(ctx) {
    const { id } = ctx.params;
    try {
      let result = await this.findInterfaceById(id);
      const res = this.addResInfo({
        type: 1,
        msg: "找不到该上映计划",
        result
      });
      return {
        ...res,
        data: { schedule: result }
      };
    } catch (error) {
      return this.addResInfo({
        error,
        msg: "获取上映计划失败"
      });
    }
  }

  async delById(ctx) {
    const { id } = ctx.params;
    try {
      const result = await this.delInterfaceById(id);
      const res = this.addResInfo({
        type: 1,
        msg: "计划不存在",
        result
      });
      return {
        ...res,
        data: result
      };
    } catch (error) {
      return this.addResInfo({ error, msg: "上映计划删除失败" });
    }
  }

  async getList() {
    try {
      const result = await this.findListInterface();
      const res = this.addResInfo({
        type: 1,
        msg: "获取上映计划列表失败",
        result
      });
      const schedules = this.changAttrs(result, { scheduleId: "id" });
      return {
        ...res,
        data: { schedules }
      };
    } catch (error) {
      return this.addResInfo({ error, msg: "获取上映计划列表失败" });
    }
  }
}

const service = new Service();
module.exports = service;
